// 初始化腾讯云
const app = tcb.init({
    env: 'dp-8gxhizpn6b2e10ce' // 更新为您的环境ID
});
const db = app.database();

// 发布订单
async function publishOrder() {
    try {
        const order = {
            title: document.getElementById('title').value,
            description: document.getElementById('description').value,
            price: document.getElementById('price').value,
            status: 'open',
            createTime: Date.now(),
            publisher: '用户ID'
        };
        
        await db.collection('orders').add(order);
        alert('发布成功！');
        loadOrders();
    } catch (err) {
        console.error('发布失败：', err);
        alert('发布失败，请重试');
    }
}

// 获取订单列表
async function loadOrders() {
    try {
        const result = await db.collection('orders')
            .where({
                status: 'open'
            })
            .orderBy('createTime', 'desc')
            .get();
            
        const orderList = document.getElementById('orderList');
        orderList.innerHTML = '';
        
        result.data.forEach(order => {
            orderList.innerHTML += `
                <div class="order-item">
                    <h3>${order.title}</h3>
                    <p>${order.description}</p>
                    <p>报酬: ¥${order.price}</p>
                    <button onclick="acceptOrder('${order._id}')">接单</button>
                </div>
            `;
        });
    } catch (err) {
        console.error('加载订单失败：', err);
    }
}

// 接单
async function acceptOrder(orderId) {
    try {
        await db.collection('orders').doc(orderId).update({
            status: 'accepted',
            acceptTime: Date.now(),
            acceptUser: '接单用户ID'
        });
        alert('接单成功！');
        loadOrders();
    } catch (err) {
        console.error('接单失败：', err);
        alert('接单失败，请重试');
    }
}

// 页面加载完成后获取订单列表
window.onload = loadOrders; 